package pure.sound
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

public class Sfx extends SoundItem
{
	public function Sfx()
	{
		super();
		this._soundChannels=new Vector.<SoundChannel>();
	}

	protected var _soundChannels			:Vector.<SoundChannel>;	/*音轨(集合)*/

	/**
	 * 从开始播放
	 * @loop				循环次数
	 */	
	public function play(loop:int = 1):void
	{
		//super.play();
		/*if(!this._isPreload){
			loadSound();
		}*/
		
		var soundChannel:SoundChannel= this._sound.play(0, loop, this._soundTransform);
		if(soundChannel){
			soundChannel.addEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
			this._soundChannels.push(soundChannel);
			if(!this._isPlaying){
				this._isPlaying = true;
			}
		}
		else{
			throw new Error("<URL::"+this._url+">声音文件出错!");
		}
	}
	
	/**
	 * 停止
	 * @isPaused			是否为暂停
	 */	
	public function stop() : void
	{
		//super.stop();
		if(!this._isPlaying){
			return;
		}
		this._isPlaying = false;
		
		var temp:SoundChannel=null;
		var len:int=this._soundChannels.length;
		for(var i:int=len-1;i>=0;i--){
			temp=this._soundChannels.pop();
			temp.removeEventListener(Event.SOUND_COMPLETE,____onPlayFinished);
			temp.stop();
			temp=null;
		}
	}
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	/*调节音量*/
	override public function set volume(value:Number):void
	{
		super.volume=value;
		var temp:SoundChannel=null;
		var len:int=this._soundChannels.length;
		for(var i:int=0;i<len;i++){
			temp=this._soundChannels[i];
			temp.soundTransform=this._soundTransform;
		}
	}
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	

	
	protected function ____onPlayFinished(e:Event = null) : void
	{
		var temp:SoundChannel=e.target as SoundChannel;
		temp.removeEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
		var num:int=this._soundChannels.indexOf(temp);
		if(!this._soundChannels.splice(num,1).length){
			this._isPlaying=false;
		}
		temp=null;
	}
}
}